//
//  MAPYLaunchView.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYLaunchView: UIView {
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        addSubview(mapy_bgView)
       
        addSubview(mapy_titleView)
        addSubview(mapy_progressBgView)
        mapy_progressBgView.addSubview(mapy_progressView)
        mapy_bgView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalToSuperview().offset(-50)
        }
        mapy_progressBgView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 92, height: 4))
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-54)
        }
        mapy_titleView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 134, height: 24))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(mapy_progressBgView.snp.top).offset(-25)
        }

    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - mapy action
    
    func mapy_hiddenView() {
        mapy_hiddenBlcok?()
        UIView.animate(withDuration: 0.25) {
            self.alpha = 0
        } completion: { _ in
            
        }
    }
    
    func mapy_startLoading() {
        mapy_value = 0
        mapy_speed = 10
        isHidden = false
        alpha = 1
        mapy_timeName = MAPYTimerCommon.mapy_timeTask(self, selector: #selector(mapy_timeSum), start: 0, interval: 0.01, repeats: true, async: false, name: mapy_timeName)
    }
    
    func mapy_stopLoading() {
        MAPYTimerCommon.mapy_cancelTask(mapy_timeName)
        mapy_value = 1
        mapy_hiddenView()
    }
    
    func mapy_cancelLoading() {
        MAPYTimerCommon.mapy_cancelTask(mapy_timeName)
        mapy_value = 0
        isHidden = true
    }
    
    @objc func mapy_timeSum()  {
        if mapy_value >= 92 {
            mapy_stopLoading()
            return
        }
        mapy_value += mapy_step * mapy_speed
    }
    
    
    //MARK: - mapy po
    var mapy_value: CGFloat = 0 {
        didSet {
            mapy_progressView.frame = CGRect(x: 0, y: 0, width: mapy_value, height: 4)
        }
    }
    var mapy_speed: CGFloat = 1
    var mapy_timeName: String = "mapy_timeName"
    var mapy_hiddenBlcok:(()->())?
    var mapy_isShowAd = false
    var mapy_step:CGFloat = 92 / CGFloat(15) * 0.01
    //MARK: - mapy lazy
    lazy var mapy_bgView: UIImageView = {
        let mapy_bgView = UIImageView(image: UIImage(named: "mapy_launch_bg"))
        mapy_bgView.contentMode = .scaleAspectFill
        return mapy_bgView
    }()
   
    lazy var mapy_titleView: UIImageView = {
        let mapy_titleView = UIImageView(image: UIImage(named: "mapy_launch_title"))
        return mapy_titleView
    }()
    lazy var mapy_progressBgView: UIView = {
        let mapy_progressBgView = UIView()
        mapy_progressBgView.backgroundColor = MAPYCommon.mapy_getHXColorValue(rgb: 0x3E1B0A)
        return mapy_progressBgView
    }()
    lazy var mapy_progressView: UIImageView = {
        let mapy_progressView = UIImageView(image: UIImage(named: "mapy_launch_progress"))
        return mapy_progressView
    }()
}

